/**
 * Copyright By Grandsoft Company Limited. 
 * @author zhaoxj
 * @date 2015年7月16日
 */
package org.suren.littlebird.editors;

import java.util.Date;
import java.util.Properties;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
import org.suren.littlebird.po.BundleHeader;
import org.suren.littlebird.po.ServiceInfo;
import org.suren.littlebird.po.SuRenBundle;

/**
 * 查看bundle详情
 * 
 * @author zhaoxj
 * 
 * @2015年7月16日
 */
public class BundleEditor extends EditorPart {

	private TabFolder mainCom;
	private SuRenBundle bundle;
	
	@Override
	public void createPartControl(Composite parent) {
		mainCom = new TabFolder(parent, SWT.None);
		
		IEditorInput input = getEditorInput();
		if(!(input instanceof BundleEditorInput))
		{
			System.err.println(input + " is not BundleEditorInput.");
			return;
			
		}

		BundleEditorInput bundleInput = (BundleEditorInput) input;
		BundleStorage storage = bundleInput.getBundleStorage();
		bundle = storage.getSuRenBundle();
		if(bundle == null)
		{
			return;
		}
		
		createMetaData();
		
		createHeadrs();
		
		createServiceInfo();
		
		setPartName(storage.getName());
	}

	/**
	 * 
	 */
	private void createMetaData() {
		TabItem metaDataItem = new TabItem(mainCom, SWT.None);
		metaDataItem.setText("MetaData");
		
		Composite metaDataComposite = new Composite(mainCom, SWT.None);
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		metaDataComposite.setLayout(gridLayout);
		metaDataItem.setControl(metaDataComposite);
		
		if(bundle == null)
		{
			return;
		}
		
		new Label(metaDataComposite, SWT.None).setText("Id");
		new Label(metaDataComposite, SWT.None).setText(String.valueOf(bundle.getId()));
		
		new Label(metaDataComposite, SWT.None).setText("Name");
		new Label(metaDataComposite, SWT.None).setText(bundle.getName());
		
		new Label(metaDataComposite, SWT.None).setText("Level");
		new Label(metaDataComposite, SWT.None).setText(String.valueOf(bundle.getLevel()));
		
		new Label(metaDataComposite, SWT.None).setText("Version");
		new Label(metaDataComposite, SWT.None).setText(bundle.getVersion());

		new Label(metaDataComposite, SWT.None).setText("LastModified");
		new Label(metaDataComposite, SWT.None).setText(new Date(bundle.getLastModified()).toLocaleString());
		
		new Label(metaDataComposite, SWT.None).setText("State");
		new Label(metaDataComposite, SWT.None).setText(String.valueOf(bundle.getState()));
		
		new Label(metaDataComposite, SWT.None).setText("Location");
		new Label(metaDataComposite, SWT.None).setText(bundle.getLocation());
	}

	/**
	 * 
	 */
	private void createHeadrs() {
		TabItem headersItem = new TabItem(mainCom, SWT.None);
		headersItem.setText("Headers");

		ScrolledComposite scrolledComposite = new ScrolledComposite(mainCom, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		Composite headersComposite = new Composite(scrolledComposite, SWT.None);
		headersComposite.setLayout(new GridLayout(2, false));
		headersItem.setControl(scrolledComposite);
		for(BundleHeader header : bundle.getHeaders())
		{
			new Label(headersComposite, SWT.None).setText(header.getKey().toString());
			new Label(headersComposite, SWT.None).setText(header.getValue().toString());
		}
		scrolledComposite.setContent(headersComposite);
		scrolledComposite.setMinSize(headersComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
	}

	/**
	 * 
	 */
	private void createServiceInfo() {
		TabItem serviceInfoItem = new TabItem(mainCom, SWT.None);
		serviceInfoItem.setText("ServiceInfo");
		
		ScrolledComposite scrolledComposite = new ScrolledComposite(mainCom, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
		Composite serviceInfoComposite = new Composite(scrolledComposite, SWT.None);
		serviceInfoComposite.setLayout(new GridLayout(2, false));
		serviceInfoItem.setControl(scrolledComposite);
		if(bundle.getServiceRef() != null)
		{
			for(ServiceInfo serviceInfo : bundle.getServiceRef())
			{
				if(serviceInfo == null)
				{
					continue;
				}
				
				Properties pro = serviceInfo.getProperties();
				for(Object key : pro.keySet())
				{
					new Label(serviceInfoComposite, SWT.WRAP).setText(key.toString());
					new Label(serviceInfoComposite, SWT.WRAP).setText(pro.get(key).toString());
				}
			}
		}

		scrolledComposite.setContent(serviceInfoComposite);
		scrolledComposite.setMinSize(serviceInfoComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
		scrolledComposite.setExpandHorizontal(true);
		scrolledComposite.setExpandVertical(true);
	}

	/*
	 * 
	 */
	@Override
	public void doSave(IProgressMonitor arg0) {
		// TODO Auto-generated method stub
		
	}

	/*
	 * 
	 */
	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub
		
	}

	/*
	 * 
	 */
	@Override
	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		setSite(site);
		setInput(input);
	}

	/*
	 * 
	 */
	@Override
	public boolean isDirty() {
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 * 
	 */
	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 * 
	 */
	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
		
	}
}
